#include "gameselect.h"
#include <QPainter>
#include <QPixmap>
#include <QIcon>
#include <QPushButton>
#include <QFont>
#include <gamehall.h>
#include <QSound>
#include <gameroom.h>
#include <QTextEdit>
#include <QFile>
#include <QTextStream>

GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
{
    //设置窗口大小
    this->setFixedSize(1000,800);

    //设置窗口图标和标题
    this->setWindowIcon(QIcon(":res/back.png"));
    this->setWindowTitle("游戏关卡选择");

    //设置返回游戏大厅按钮
    QPushButton* backBtn = new QPushButton(this);
    backBtn->move(950,750);
    backBtn->setIcon(QIcon(":res/back.png"));
    connect(backBtn,&QPushButton::clicked,this,[=](){
       this->close();
       GameHall* gamehall = new GameHall;
       gamehall->show();

       QSound::play(":res/clicked.wav");
    });

    QFont font("华文行楷",24);

    GameRoom* gameRoom = new GameRoom;

    //设置简单模式按钮
    QPushButton* simpleBtn = new QPushButton(this);
    simpleBtn->move(440,160);
    simpleBtn->setText("简单模式");
    simpleBtn->setFont(font);
    simpleBtn->setStyleSheet("QPushButton:{border: 2px solid #8f8f91; border-radius: 15px}");
    connect(simpleBtn,&QPushButton::clicked,this,[=](){
        this->close();
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();
        QSound::play(":res/clicked.wav");

        gameRoom->setTimeout(300);
    });

    //设置正常模式按钮
    QPushButton* normalBtn = new QPushButton(this);
    normalBtn->move(440,250);
    normalBtn->setText("正常模式");
    normalBtn->setFont(font);
    normalBtn->setStyleSheet("QPushButton:{border: 2px solid #8f8f91; border-radius: 15px}");
    connect(normalBtn,&QPushButton::clicked,this,[=](){
        this->close();
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();
        QSound::play(":res/clicked.wav");

        gameRoom->setTimeout(200);
    });


    //设置困难模式按钮
    QPushButton* hardBtn = new QPushButton(this);
    hardBtn->move(440,340);
    hardBtn->setText("困难模式");
    hardBtn->setFont(font);
    hardBtn->setStyleSheet("QPushButton:{border: 2px solid #8f8f91; border-radius: 15px}");
    connect(hardBtn,&QPushButton::clicked,this,[=](){
        this->close();
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();
        QSound::play(":res/clicked.wav");

        gameRoom->setTimeout(100);
    });


    //设置查看历史战绩按钮
    QPushButton* hisBtn = new QPushButton(this);
    hisBtn->move(440,430);
    hisBtn->setText("历史战绩");
    hisBtn->setFont(font);
    hisBtn->setStyleSheet("QPushButton:{border: 2px solid #8f8f91; border-radius: 15px}");

    connect(hisBtn,&QPushButton::clicked,[=](){
       QWidget* widget = new QWidget;
       widget->setWindowTitle("历史战绩");
       widget->setFixedSize(500,300);

       QTextEdit* edit = new QTextEdit(widget);
       edit->setFont(font);
       edit->setFixedSize(500,300);
       QFile file("E:/bit/qtcode/greedy_snake/snake/res/1.txt");
       file.open(QIODevice::ReadOnly);

       QTextStream in(&file);
       int data = in.readLine().toInt();

       edit->append("得分为:");
       edit->append(QString::number(data));

       widget->show();
    });

}

void GameSelect::paintEvent(QPaintEvent *event)
{
    //实例化画家对象
    QPainter painter(this);

    //实例化绘图设备
    QPixmap pix(":res/game_select.png");

    //绘画
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
}
